<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="填写时间" prop="optionId">
        <el-date-picker clearable
            v-model="queryParams.createTime"
            type="month"
            value-format="yyyy-MM"
            placeholder="请选择填表时间">
          </el-date-picker>
      </el-form-item>
      <el-form-item label="问卷名称" prop="optionId">
        <el-select v-model="queryParams.responseId"  placeholder="请选择" @change="handleQuery">
                <el-option v-for="item in surveysList"   :label="item.title" :value="item.surveyId"  ></el-option>
              </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column label="问卷名称" align="center" prop="responseName" />
      <el-table-column label="问卷链接" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <a href="http://120.46.36.226:39999/#/pages/questionnaire/questionnaire" target="_blank">http://120.46.36.226:39999/#/pages/questionnaire/questionnaire</a>
        </template>
      </el-table-column>
      <el-table-column label="填写时间" align="center" prop="createTime" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="产品型号" align="center" prop="modle" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >查看问卷结果</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户回答详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div v-for="item in obj.details" style="display: flex;padding: 8px;gap: 8px;align-items: center;">
        <div>{{ item.questionName }}:</div><el-tag v-for="it in item.optionName.split(';')">{{ it }}</el-tag><el-tag v-if="item.answerText">{{item.answerText }}</el-tag>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/yxz/details";
import { listSurveys, getSurveys, delSurveys, addSurveys, updateSurveys } from "@/api/yxz/surveys";
export default {
  name: "Details",
  data() {
    return {
      obj:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户回答详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        responseId: null,
        questionId: null,
        optionId: null,
        answerText: null,
        userId: null,
      },
      surveysList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    listSurveys({...this.queryParams,pageSize:999999}).then(response => {
        if(response.data.length>0){
          this.surveysList = this.handleTree(response.data, "surveyId", "pId");
        }
        this.queryParams.responseId=this.surveysList[0].surveyId
        this.loading = false;
      });
  },
  methods: {
    /** 查询用户回答详情列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        detailId: null,
        responseId: null,
        questionId: null,
        optionId: null,
        answerText: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.detailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户回答详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.obj=row
      this.open = true;
      this.title = "用户回答详情";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.detailId != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const detailIds = row.detailId || this.ids;
      this.$modal.confirm('是否确认删除用户回答详情编号为"' + detailIds + '"的数据项？').then(function() {
        return delDetails(detailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yxz/details/export', {
        ...this.queryParams
      }, `details_${this.surveysList.find(item=>item.surveyId==this.queryParams.responseId).title}.xlsx`)
    }
  }
};
</script>
